package ui

import org.apache.wicket.markup.html.WebPage
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import dominio.Jugador
import applicationModel.Fila
import org.apache.wicket.markup.html.list.ListItem
import org.apache.wicket.markup.html.form.Form
import applicationModel.Plantar
import org.apache.wicket.markup.html.form.DropDownChoice
import java.util.List
import dominio.Planta
import org.uqbar.wicket.xtend.XButton
import applicationModel.Ataque
import dominio.Zombie
import home.HomeZombies
import org.uqbar.commons.model.UserException
import org.apache.wicket.markup.html.panel.FeedbackPanel

class AtacarPage extends WebPage{
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	var JardinTableroPage mainPage
	var Fila filaAAtacar
	var Jugador jugador
	var Ataque ataque
	
	new(JardinTableroPage mainPageX, Fila filaX, Jugador jugadorX){
		mainPage = mainPageX
		filaAAtacar = filaX
		jugador = jugadorX
		ataque = new Ataque(filaAAtacar, jugador)
		
		val ataqueForm = new Form<Ataque>("ataqueForm", ataque.asCompoundModel)
		
		
		listarZombies(ataqueForm)
		this.addChild(ataqueForm)
		this.addChild(new FeedbackPanel("feedbackPanel"))
	}
	
	def listarZombies(Form<Ataque> form) {
		form.addChild(new DropDownChoice<Zombie>("ZombieAtacante") => [
			choices = loadableModel([| (Zombie.home.allInstances )])
			choiceRenderer = choiceRenderer([Zombie m| m.getNombre()])
		]) 
		form.addChild(new XButton("Atacar") => [
			onClick = [|this.atacarla()]])
		form.addChild(new XButton("Volver") => [
			onClick = [|this.volver()]])
		}
	
	def atacarla() {
		try{
			ataque.ataca()
			this.mensajeOk()
		}
		catch(UserException e){
			error(e.getMessage())
		}
		catch(NullPointerException e){
			error("Elige un Zombie para atacar la Fila")
		}
	}
	
	def mensajeOk() {
		 info(ataque.resultado)
	}
	
	def volver() {
		responsePage = mainPage
	}
	
	}